﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using WorkOrder.Model;
using System.Drawing;

namespace WorkOrder
{
    public partial class addChangeover : Page
    {
        Business_Table_Changeover B_T_C = new Business_Table_Changeover();
        private String HtmlCode(string TString)
        {
            if (TString != null)
            {
                TString = TString.Replace("\r", " <br/> ");
                TString = TString.Replace(" ", "&nbsp;");
                return TString;
            }
            return "空";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
            
            Model_Table_Changeover model;
            if (!Page.IsPostBack)
            {
                #region 加载资料
                ddlProduct.Items.Clear();
                ddlProduct.DataTextField = "product_name";
                ddlProduct.DataValueField = "product_id";
                ddlProduct.DataSource = B_T_C.GetProduct();
                ddlProduct.DataBind();
                ddlProduct.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;

                seleTown.Items.Clear();
                seleTown.DataTextField = "town_name";
                seleTown.DataValueField = "town_id";
                seleTown.DataSource = B_T_C.GetTown();
                seleTown.DataBind();
                seleTown.Items.Add(new ListItem("-- 请选择 --", "0"));
                seleTown.SelectedIndex = seleTown.Items.Count - 1;

                ddlFactory.Items.Clear();
                ddlFactory.DataTextField = "fact_name";
                ddlFactory.DataValueField = "fact_id";
                ddlFactory.DataSource = B_T_C.GetFactory();
                ddlFactory.DataBind();
                ddlFactory.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlFactory.SelectedIndex = ddlFactory.Items.Count - 1;

                ddlSupervision.Items.Clear();
                ddlSupervision.DataTextField = "supe_unit_name";
                ddlSupervision.DataValueField = "supe_unit_id";
                ddlSupervision.DataSource = B_T_C.GetSupervisionUnit();
                ddlSupervision.DataBind();
                ddlSupervision.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlSupervision.SelectedIndex = ddlSupervision.Items.Count - 1;

                ddlIntegrated.Items.Clear();
                ddlIntegrated.DataTextField = "inte_unit_name";
                ddlIntegrated.DataValueField = "inte_unit_id";
                ddlIntegrated.DataSource = B_T_C.GetIntegratedUnit();
                ddlIntegrated.DataBind();
                ddlIntegrated.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlIntegrated.SelectedIndex = ddlIntegrated.Items.Count - 1;

                ddlDebuggion.Items.Clear();
                ddlDebuggion.DataTextField = "debug_unit_name";
                ddlDebuggion.DataValueField = "debug_unit_id";
                ddlDebuggion.DataSource = B_T_C.GetDebuggingUnit();
                ddlDebuggion.DataBind();
                ddlDebuggion.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlDebuggion.SelectedIndex = ddlDebuggion.Items.Count - 1;

                ddlCutProblem.Items.Clear();
                ddlCutProblem.DataTextField = "changeover_problems_type_name";
                ddlCutProblem.DataValueField = "changeover_problems_type_id";
                ddlCutProblem.DataSource = B_T_C.GetChangeoverProblemsType();
                ddlCutProblem.DataBind();
                ddlCutProblem.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlCutProblem.SelectedIndex = ddlCutProblem.Items.Count - 1;

                ddlStatus.Items.Clear();
                ddlStatus.DataTextField = "changeover_status";
                ddlStatus.DataValueField = "changeover_status_id";
                ddlStatus.DataSource = B_T_C.GetStatus();
                ddlStatus.DataBind();
                ddlStatus.Items.Add(new ListItem("-- 请选择 --", "0"));
                ddlStatus.SelectedIndex = ddlStatus.Items.Count - 1;

                #endregion
            }
            else
            {
                if (hidID.Value == "")
                {
                    if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Changeover"))
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：您没有新增割接单信息的权限！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您没有新增割接单信息的权限！');</script>");
                        return;
                    }
                    if (B_T_C.HasExist(txtBusinessOrderNo.Text.Trim()))
                    {
                        lblMsg.ForeColor = Color.Red;
                        lblMsg.Text = "提示信息：业务单号为" + txtBusinessOrderNo.Text.Trim() + "的记录已经存在，请重新输入！";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('提示信息：业务单号为" + txtBusinessOrderNo.Text.Trim() + "的记录已经存在，请重新输入！');</script>");
                        return;
                    }
                    else
                    {
                        if (hidTown.Value == "") hidTown.Value = "NULL";
                        DateTime creattime = DateTime.Now; 
                        int tmp = 0;
                        string order_id = Guid.NewGuid().ToString();
                        try { creattime = Convert.ToDateTime(txtFirsetCutTime.Text.Trim()); tmp = 1; }
                        catch { tmp = 0; }
                        if (tmp != 1)
                        {
                            lblMsg.ForeColor = Color.Red;
                            lblMsg.Text = "提示信息：初次进场割接时间格式错误，请重新输入！";
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "winf", "<script type='text/javascript'>alert('提示信息：初次进场割接时间格式错误，请重新输入！');</script>");
                            return;
                        }
                        if (txtReturnVisit.Value.Trim() != "")
                        {
                            try { Convert.ToDateTime(txtReturnVisit.Value.Trim()); tmp = 1; }
                            catch { tmp = 0; }
                            if (tmp != 1)
                            {
                                lblMsg.ForeColor = Color.Red;
                                lblMsg.Text = "提示信息：最终回访时间格式错误，请重新输入！";
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "winf", "<script type='text/javascript'>alert('提示信息：最终回访时间格式错误，请重新输入！');</script>");
                                return;
                            }
                        }
                        model = new Model_Table_Changeover();
                        model.Changeover_id = order_id;
                        model.Changeover_number = Server.HtmlEncode(txtOrderNo.Value.Trim());
                        model.Area_id = seleArea.Value;
                        model.Customer_address = Server.HtmlEncode(txtCustomerAdd.Value.Trim());
                        model.Debug_operator = Server.HtmlEncode(txtDebuggion.Value.Trim());
                        model.Customer_name = Server.HtmlEncode(txtCustomer.Value.Trim());
                        model.Customer_tell = Server.HtmlEncode(txtCustomerTell.Value.Trim());
                        model.Debug_unit_id = ddlDebuggion.SelectedValue;
                        model.Equipment_model = Server.HtmlEncode(txtModel.Value.Trim());
                        model.Factory_id = ddlFactory.SelectedValue;
                        model.Status_id = ddlStatus.SelectedValue;
                        model.First_changeover_time = Server.HtmlEncode(txtFirsetCutTime.Text.Trim());
                        model.Integrated_operator = Server.HtmlEncode(txtIntegrated.Value.Trim());
                        model.Integrated_unit_id = ddlIntegrated.SelectedValue;
                        model.Product_id = ddlProduct.SelectedValue;
                        model.Station_name = Server.HtmlEncode(txtStation.Text.Trim());
                        model.Supervision_operator = Server.HtmlEncode(txtSupervision.Value.Trim());
                        model.Supervision_unit_id = ddlSupervision.SelectedValue;
                        model.Town_id = hidTown.Value;
                        model.UserID = Session["UserID"].ToString();
                        model.Order_number = Server.HtmlEncode(txtBusinessOrderNo.Text.Trim());
                        if (B_T_C.Insert(model))
                        {
                            hidID.Value = model.Changeover_id;

                            if (ddlStatus.SelectedValue != "0")
                                B_T_C.UpdateStatus(hidID.Value, ddlStatus.SelectedValue);

                            if (ddlCutProblem.SelectedValue != "0")
                            {
                                if (txtProStartTime.Value.Trim() == "")
                                    txtProStartTime.Value = DateTime.Now.ToString();
                                B_T_C.AddChangeoverProblems(hidID.Value, ddlCutProblem.SelectedValue, Server.HtmlEncode(txtCutDesc.Text.Trim()), Server.HtmlEncode(txtProStartTime.Value.Trim()), null);
                            }

                            seleArea.Value = "0";
                            seleTown.Value = "0";
                            txtOrderNo.Value = "";
                            txtCustomerAdd.Value = "";
                            txtCustomer.Value = "";
                            txtCustomerTell.Value = "";
                            ddlDebuggion.SelectedIndex = ddlDebuggion.Items.Count - 1;
                            txtModel.Value = "";
                            ddlFactory.SelectedIndex = ddlFactory.Items.Count - 1;
                            txtFirsetCutTime.Text = "";
                            txtIntegrated.Value = "";
                            ddlIntegrated.SelectedIndex = ddlIntegrated.Items.Count - 1;
                            ddlProduct.SelectedIndex = ddlProduct.Items.Count - 1;
                            txtStation.Text = "";
                            txtSupervision.Value = "";
                            ddlSupervision.SelectedIndex = ddlSupervision.Items.Count - 1;
                            ddlStatus.SelectedIndex = ddlStatus.Items.Count - 1;
                            txtDebuggion.Value = "";
                            txtProStartTime.Value = "";
                            txtCutDesc.Text = "";
                            txtBusinessOrderNo.Text = "";
                            ddlCutProblem.SelectedIndex = ddlCutProblem.Items.Count - 1;
                            lblMsg.ForeColor = Color.DarkGreen;
                            lblMsg.Text = "提示信息：成功保存一条记录！";
                        }
                    }
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (B_T_C.IsExist(txtBusinessOrderNo.Text.Trim()))
            {
                Label1.Text = "* 业务单号已存在！";
            }
            else
            { Label1.Text = ""; }
        }
    }
}
